package com.bluetooth.lib.util;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.os.Environment;

/**
 * @author breaktian
 * 20150706
 * file工具类
 * */
public class F {
	/**
	 * 获得外部存储根目录
	 * */
	public static String getExternalPath(){
	       boolean sdCardExist = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);   //判断sd卡是否存在
	       if(sdCardExist)  
	       {                              
	            File sdDir = Environment.getExternalStorageDirectory();//获取跟目录
	            return sdDir.toString();
	       }  
	       return null;
	}
	
	/**
	 * 创建多级目录
	 * */
	public static boolean createDirs(String dir){
		File file = new File(dir);
		if(!file.exists()){//如果目录不存在
			file.mkdirs();//创建多级目录
			if(file.exists()){
				return true;
			}else{
				return false;
			}
		}
		return true;
	}
	
	
	/**
	 * 递归遍历某一目录下所有文件 包含其子目录的文件
	 * param:
	 * file-要遍历的目录
	 * listFiles 存放File的容器
	 * return:listFiles
	 * */
	public static ArrayList<File> searchFiles(File file,ArrayList<File> listFiles){
		if(!file.isFile()){
			File[] list = file.listFiles();
			for(int i=0;i<list.length;i++){
				File f = list[i];
				if(f.isDirectory()){
					searchFiles(f,listFiles);//递归
				}else{
					listFiles.add(f);
				}
			}
		}
		return listFiles;
	}
	
	
	/**
	 * 根据路径删除指定文件
	 * param:
	 * path-文件路径
	 * return:boolean
	 * */
	public static boolean delFile(String path){
		File file = new File(path);
		if(file.exists()){
			if(file.isFile()){//是文件
				file.delete();
			}
		}
		return false;
	}
	
	
	/**
	 * 清空某一目录
	 * param:
	 * dir-目录
	 * */
	public static void clearFiles(String dir){
		File file = new File(dir);
		if(file.isDirectory()){
			File[] arr = file.listFiles();
			for(int i=0;i<arr.length;i++){
				if(arr[i].isFile()){
					arr[i].delete();
				}else{
					//清空子目录下的文件
					clearFiles(arr[i].getAbsolutePath());
					//删除子目录
					arr[i].delete();
				}
			}
		}
	}
	
	
	
	
	
	/** 
     * 获得指定文件的byte数组 
     */  
    public static byte[] getBytes(File file){  
        byte[] buffer = null;  
        try {  
            FileInputStream fis = new FileInputStream(file);  
            ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);  
            byte[] b = new byte[1000];  
            int n;  
            while ((n = fis.read(b)) != -1) {  
                bos.write(b, 0, n);  
            }  
            fis.close();  
            bos.close();  
            buffer = bos.toByteArray();  
        } catch (FileNotFoundException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
        return buffer;  
    }
    
    /** 
     * 获得指定文件的byte数组 
     */  
    public static byte[] getBytes(String filePath){  
    	byte[] buffer = null;  
    	try {  
    		File file = new File(filePath);  
    		FileInputStream fis = new FileInputStream(file);  
    		ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);  
    		byte[] b = new byte[1000];  
    		int n;  
    		while ((n = fis.read(b)) != -1) {  
    			bos.write(b, 0, n);  
    		}  
    		fis.close();  
    		bos.close();  
    		buffer = bos.toByteArray();  
    	} catch (FileNotFoundException e) {  
    		e.printStackTrace();  
    	} catch (IOException e) {  
    		e.printStackTrace();  
    	}  
    	return buffer;  
    }
    
    /** 
     * 根据byte数组，生成文件 
     */  
    public static void createFile(byte[] bfile, File file) {  
        BufferedOutputStream bos = null;  
        FileOutputStream fos = null;  
        try {  
            fos = new FileOutputStream(file);  
            bos = new BufferedOutputStream(fos);  
            bos.write(bfile);  
        } catch (Exception e) {  
            e.printStackTrace();  
        } finally {  
            if (bos != null) {  
                try {  
                    bos.close();  
                } catch (IOException e1) {  
                    e1.printStackTrace();  
                }  
            }  
            if (fos != null) {  
                try {  
                    fos.close();  
                } catch (IOException e1) {  
                    e1.printStackTrace();  
                }  
            }  
        }  
    }  
    
    /** 
     * 根据byte数组，生成文件 
     */  
    public static void createFile(byte[] bfile, String filePath,String fileName) {  
    	BufferedOutputStream bos = null;  
    	FileOutputStream fos = null;  
    	File file = null;  
    	try {  
    		File dir = new File(filePath);  
    		if(!dir.exists()&&dir.isDirectory()){//判断文件目录是否存在  
    			dir.mkdirs();  
    		}  
    		file = new File(filePath+"\\"+fileName);  
    		fos = new FileOutputStream(file);  
    		bos = new BufferedOutputStream(fos);  
    		bos.write(bfile);  
    	} catch (Exception e) {  
    		e.printStackTrace();  
    	} finally {  
    		if (bos != null) {  
    			try {  
    				bos.close();  
    			} catch (IOException e1) {  
    				e1.printStackTrace();  
    			}  
    		}  
    		if (fos != null) {  
    			try {  
    				fos.close();  
    			} catch (IOException e1) {  
    				e1.printStackTrace();  
    			}  
    		}  
    	}  
    }  
    
    /**
     * 向文件中写入一行数据
     * 追加
     * */
    public static void writeLine(String s, File file){
    	 FileWriter fw = null;
    	 BufferedWriter writer = null;
         try {
             fw = new FileWriter(file,true);
             writer = new BufferedWriter(fw);
             writer.write(s);
             writer.newLine();//换行
             writer.flush();
         } catch (FileNotFoundException e) {
             e.printStackTrace();
         }catch (IOException e) {
             e.printStackTrace();
         }finally{
             try {
                 writer.close();
                 fw.close();
             } catch (IOException e) {
                 e.printStackTrace();
             }
         }
    }
    
    /**
     * 向文件中写入一行数据
     * 重新写入 不追加
     * */
    public static void writeLineNew(String s, File file){
    	FileWriter fw = null;
    	BufferedWriter writer = null;
    	try {
    		fw = new FileWriter(file,false);
    		writer = new BufferedWriter(fw);
    		writer.write(s);
    		writer.newLine();//换行
    		writer.flush();
    	} catch (FileNotFoundException e) {
    		e.printStackTrace();
    	}catch (IOException e) {
    		e.printStackTrace();
    	}finally{
    		try {
    			writer.close();
    			fw.close();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}
    }
    
    /**
     * 按行读取数据
     * */
    public static List<String> readLine(File file){
    	List<String> list = new ArrayList<String>();
    	FileReader fr = null;
    	BufferedReader br = null;
    	try {
    		fr = new FileReader(file);
    		br = new BufferedReader(fr);
    		String line = "";
    		while((line=br.readLine())!=null){
    			if(line.length()>0){
    				list.add(line);
    			}
    		}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				br.close();
				fr.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
    	return list;
    }
    
    /**
     * 按照字符串匹配删除整行数据
     * */
    public static void delLine(String s, File file){
    	List<String> list = readLine(file);
    	for(int i=0;i<list.size();i++){
    		String ss = list.get(i);
    		if(ss.startsWith(s)){
    			list.remove(i);
    			break;
    		} 
    	}
    	
    	if(list.size()<1){
    		writeLineNew("", file);
    	}
    	
    	for (int i=0;i<list.size();i++) {
			if(i==0){
				writeLineNew(list.get(i), file);
			}else{
				writeLine(list.get(i), file);//追加
			}
		}
    	
    	
    }
    
    
	
	
}